package com.baomidou.ant.service.impl;

import com.baomidou.ant.entity.*;
import com.baomidou.ant.mapper.PinLibraryMapper;
import com.baomidou.ant.service.*;
import com.baomidou.ant.utils.MyStringUtil;
import com.baomidou.ant.vo.request.RequestListPinVo;
import com.baomidou.ant.vo.response.ResponseListPinVo;
import com.baomidou.ant.vo.response.ResponseListProcessInfoVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Service
public class PinLibraryServiceImpl extends ServiceImpl<PinLibraryMapper, PinLibrary> implements IPinLibraryService {
    @Autowired
    private ISysUserService userService;
    @Autowired
    private ISysDeptService deptService;
    @Autowired
    private IProcessInfoService processInfoService;
    @Autowired
    private IProcessConditionService processConditionService;

    @Override
    public IPage<ResponseListPinVo> listPin(RequestListPinVo requestListPinVo) {
        IPage<PinLibrary> iPage=new Page<>(requestListPinVo.getDisplayStart(),requestListPinVo.getDisplayLength());
        QueryWrapper<PinLibrary> queryWrapper=new QueryWrapper<>();
        //品名查询
        if(StringUtils.isNotBlank(requestListPinVo.getPinName())){
            queryWrapper.like("pin_name",requestListPinVo.getPinName());
        }
        SysUser user=userService.getById(requestListPinVo.getUserId());
        //判断管理员级别
        if(Objects.equals(user.getType(), "1")){
            //管理员条件查询
            if(StringUtils.isNotBlank(requestListPinVo.getDeptName())){
                Integer deptId=deptService.queryByDeptName(requestListPinVo.getDeptName());
                queryWrapper.eq("dept_id",deptId);
            }
        }else{
            queryWrapper.eq("dept_id",user.getDeptId());
        }
        //得到结果集
        IPage<PinLibrary> pinPage=page(iPage,queryWrapper);
        return pinPage.convert(pinLibrary -> {
            ResponseListPinVo vo=new ResponseListPinVo();
            BeanUtils.copyProperties(pinLibrary,vo);
            vo.setDeptName(deptService.getById(pinLibrary.getDeptId()).getDept());
            if(ObjectUtils.isEmpty(pinLibrary.getErpId())){
                vo.setErpId("未同步ERP物料Id");
            }else
                vo.setErpId(pinLibrary.getErpId());
            return vo;
        });
    }

    @Override
    public ResponseListProcessInfoVo listProcessInfo(Integer pinId) {
        PinLibrary pinLibrary=getById(pinId);

        int[] processIds= MyStringUtil.toInt(pinLibrary.getProcess());
        String[] process=new String[processIds.length];
        String[] conditionInfo=new String[processIds.length];
        for(int i=0;i<processIds.length;i++){
            ProcessInfo processInfo=processInfoService.getById(processIds[i]);
            process[i]=processInfo.getProcessName();
            QueryWrapper<ProcessCondition> queryWrapper=new QueryWrapper<>();
            queryWrapper.eq("pin_id",pinId);
            List<ProcessCondition> list=processConditionService.list(queryWrapper);
            if(list==null||list.size()==0){
                conditionInfo[i]="无";
            }else {
                conditionInfo[i]=list.get(list.size()-1).getMouldPath();
            }
        }
        ResponseListProcessInfoVo vo=new ResponseListProcessInfoVo();
        vo.setPinName(pinLibrary.getPinName());
        vo.setProcess(process);
        vo.setConditionInfo(conditionInfo);
        return vo;
    }

    @Override
    public PinLibrary queryByPinName(String pinName,Integer deptId) {
        QueryWrapper<PinLibrary> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("pin_name",pinName);
        queryWrapper.eq("dept_id",deptId);
        return getOne(queryWrapper);
    }

    @Override
    public PinLibrary queryByPinName(String pinName) {
        QueryWrapper<PinLibrary> queryWrapper=new QueryWrapper<>();
        queryWrapper.like("pin_name",pinName);
        return getOne(queryWrapper);
    }

    @Override
    public String[] queryByUserId(Integer userId) {
        SysUser user=userService.getById(userId);
        //超级管理员获取
        if(Objects.equals(user.getType(), "1")){
            List<PinLibrary> pinLibraryList=list();
            String[] pinNames=new String[pinLibraryList.size()];
            for(int i=0;i< pinLibraryList.size();i++){
                //获取所有产品名,没和erp绑定的不获取
                if(ObjectUtils.isNotEmpty(pinLibraryList.get(i).getErpId())){
                    pinNames[i]=pinLibraryList.get(i).getPinName();
                }
            }
            return pinNames;
        }else{
            QueryWrapper<PinLibrary> queryWrapper=new QueryWrapper<>();
            queryWrapper.eq("dept_id",user.getDeptId());
            List<PinLibrary> pinLibraryList=list(queryWrapper);
            String[] pinNames=new String[pinLibraryList.size()];
            for(int i=0;i< pinLibraryList.size();i++){
                //获取所有产品名,没和erp绑定的不获取
                if(ObjectUtils.isNotEmpty(pinLibraryList.get(i).getErpId())){
                    pinNames[i]=pinLibraryList.get(i).getPinName();
                }
            }
            return pinNames;
        }
    }

    @Override
    public String[] queryByDeptName(String deptName) {
        Integer deptId=deptService.queryByDeptName(deptName);
        QueryWrapper<PinLibrary> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("dept_id",deptId);
        List<PinLibrary> pinLibraryList=list(queryWrapper);
        String[] pinNames=new String[pinLibraryList.size()];
        for(int i=0;i< pinLibraryList.size();i++){
            //获取产品名
            pinNames[i]=pinLibraryList.get(i).getPinName();
        }
        return pinNames;
    }

    @Override
    public PinLibrary queryOne(Integer deptId, String Fspecification, String FMaterialId) {
        QueryWrapper<PinLibrary> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("dept_id",deptId);
        queryWrapper.eq("pin_name",Fspecification);
        queryWrapper.eq("erp_id",FMaterialId);
        if(list(queryWrapper)==null||list(queryWrapper).size()==0){
            return null;
        } else
            return list(queryWrapper).get(0);
    }

}
